#!/bin/bash
#
# Copyright (C) 2013-2021 Spring Studio
# Copyright (C) 2013-2021 Dou Dongchun <doudongchun@163.com>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

REMOTE_REPO=$1
LOCAL_WORKSPACE=$2
GIT_COMMIT=$3

set -e

echo "========================================"
echo "[*] git $(basename $LOCAL_WORKSPACE)"
echo "========================================"

if [ -z "$REMOTE_REPO" ] || [ -z "$LOCAL_WORKSPACE" ]; then
  echo "invalid call pull-repo-base.sh '$REMOTE_REPO' '$LOCAL_WORKSPACE' '$GIT_COMMIT'"
  exit 0
elif [ ! -d "$LOCAL_WORKSPACE" ]; then
  git --version
  git clone $REMOTE_REPO $LOCAL_WORKSPACE
else
  echo "The '$LOCAL_WORKSPACE' already exists."
fi

if [ ! -z "$GIT_COMMIT" ]; then
  cd $LOCAL_WORKSPACE
  git checkout .
  git checkout $GIT_COMMIT
  cd -
else
  cd $LOCAL_WORKSPACE
  git checkout .
  git checkout master
  cd -
fi
